package com.aizuda.boot.modules.flw.flow;

import com.aizuda.bpm.engine.FlowDataTransfer;
import com.aizuda.bpm.engine.core.FlowCreator;
import com.aizuda.bpm.engine.entity.FlwTaskActor;
import com.aizuda.service.web.UserSession;

import java.util.List;

/**
 * 工作流辅助类
 */
public class FlowHelper {

    public static FlowCreator getFlowCreator() {
        UserSession userSession = UserSession.getLoginInfo();
        return FlowCreator.of(userSession.getUserId(), userSession.getUsername());
    }

    public static FlwTaskActor getFlwTaskActor() {
        UserSession userSession = UserSession.getLoginInfo();
        return FlwTaskActor.ofUser(null, userSession.getUserId(), userSession.getUsername());
    }

    /**
     * 传递流程审批内容
     */
    public static void setProcessApprovalOpinion(String content) {
        FlowDataTransfer.put("processApprovalOpinion", content);
    }

    public static String getProcessApprovalOpinion() {
        return FlowDataTransfer.get("processApprovalOpinion");
    }

    public static void removeProcessApprovalOpinion() {
        FlowDataTransfer.removeByKey("processApprovalOpinion");
    }

    /**
     * 传递任务干预操作标识
     */
    public static void setTaskIntervene(Boolean intervene) {
        if (Boolean.TRUE.equals(intervene)) {
            FlowDataTransfer.put("taskIntervene", true);
        } else {
            FlowDataTransfer.removeByKey("taskIntervene");
        }
    }

    public static Boolean getTaskIntervene() {
        return FlowDataTransfer.get("taskIntervene");
    }

    public static void removeTaskIntervene() {
        FlowDataTransfer.removeByKey("taskIntervene");
    }

  /**
   * 传递当前通知需要的任务参与者列表（通过线程变量）
   */
  @SuppressWarnings("unchecked")
  public static List<FlwTaskActor> getNotifyTaskActors() {
    return FlowDataTransfer.get("notifyTaskActors");
  }

  public static void setNotifyTaskActors(List<FlwTaskActor> actors) {
    if (actors == null || actors.isEmpty()) {
      FlowDataTransfer.removeByKey("notifyTaskActors");
      return;
    }
    FlowDataTransfer.put("notifyTaskActors", actors);
  }

  public static void removeNotifyTaskActors() {
    FlowDataTransfer.removeByKey("notifyTaskActors");
  }
}
